<?php

/**
 * uniBoard Default View Class
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_output{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Page specific JS Code
	 *
	 * @var string
	 */
	
	public $js_code			= '';

	/**
	 * Style Images list
	 *
	 * @var array
	 */
	
	private $images			= array();
	
	/**
	 * Events Template
	 *
	 * @var object
	 */
	
	private $events_tpl		= NULL;
	
	/**
	 * Displayed error messages
	 *
	 * @var array
	 */
	
	public $msg_error		= NULL;
	
	/**
	 * Displayed non-error messages
	 *
	 * @var array
	 */
	
	public $msg_misc		= NULL;
	
	/**
	 * Draw Quick-Login form
	 *
	 * @var bool
	 */
	
	public $show_login		= true;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Application
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
	
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Set default style
		$this -> setStyle( $this -> appCore -> user['style'], $this -> appCore -> user['imageset']);
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Full Page
	 *
	 * @param string $page_title
	 * @param array $path
	 * @param string $content
	 * @param bool $draw_events
	 */
	
	function draw( $page_title, $path, $content, $draw_events = true){
		
		// Send headers
		$this -> sendHeaders();
		
		// Load templates
		$wrappers = new tpl_global( $this -> appCore);
		
		// Wrapper values
		$wrapper_vars = array(
			'page_css' 				=> htmlspecialchars( STYLES_DATA_PATH . 'style_' . appCore::$style . '.css'),
			'page_title' 			=> htmlspecialchars( isset( $page_title[0]) ? $this -> appCore -> conf['board_name'] . ' - ' . $page_title : $this -> appCore -> conf['board_name'] . (isset($this -> appCore -> conf['board_description'][0]) ? ' - ' . $this -> appCore -> conf['board_description'] : '')),
			'meta_keywords' 		=> ( isset( $this -> appCore -> conf['meta_keywords'][0]) ? htmlspecialchars( $this -> appCore -> conf['meta_keywords']) : ''),
			'meta_description'		=> ( isset( $this -> appCore -> conf['meta_description'][0]) ? htmlspecialchars( $this -> appCore -> conf['meta_description']) : ''),
			'jquery_path'			=> JS_FRAMEWORK_PATH,
			'board_script_path'		=> JS_BOARD_PATH,
			'page_script'			=> htmlspecialchars( STYLES_DATA_PATH . 'script_' . appCore::$style . '.js'),
			'js_code' 				=> $this -> js_code,
			'page_favicon' 			=> htmlspecialchars( isset( $this -> appCore -> conf['board_favicon'][0]) ? IMAGES_PATH . $this -> appCore -> conf['board_favicon'] : DEFAULT_FAVICON_PATH)
		);
		
		// Open Wrapper
		echo $wrappers -> tpl_wrapper_start( $wrapper_vars);
		
		// Overwrite wrapper vars with new stuff
		$wrapper_vars = array(
			'board_name' 				=> htmlspecialchars( $this -> appCore -> conf['board_name']),
			'board_description' 		=> htmlspecialchars( $this -> appCore -> conf['board_description']),
			'show_admin_link' 			=> $this -> appCore -> user_acl['is_admin'] && $this -> appCore -> conf['board_show_acp_link'],
			'website_link' 				=> htmlspecialchars( $this -> appCore -> conf['website_link']),
			'website_name' 				=> htmlspecialchars( $this -> appCore -> conf['website_name']),
			'user_id'					=> $this -> appCore -> user['id'],
			'user_name'					=> htmlspecialchars( $this -> appCore -> user['name']),
			'user_last_visit'			=> $this -> appCore -> user['last_date'],
			'show_quick_login'			=> !$this -> appCore -> banned_ip && $this -> show_login,
			'current_url' 				=> htmlspecialchars( $_SERVER['REQUEST_URI']),
			'breadcrumbs' 				=> '',
			'board_footer'				=> $this -> appCore -> conf['board_footer'],
			'time'						=> $this -> appCore -> time,
			'board_show_version'		=> $this -> appCore -> conf['board_show_version'],
			'board_version'				=> PRODUCT_VERSION_LABEL,
			'board_debug_level'			=> $this -> appCore -> conf['board_debug_level'],
			'debug_time'				=> round( microtime( true) - $this -> appCore -> gen_clock, 5),
			'debug_queries'				=> $this -> appCore -> db_queries,
			'debug_memory'				=> class_strings::countSize( memory_get_usage( true))
		);
		
		// Set User Title
		if ( isset($this -> appCore -> user['title'][0]) && $this -> appCore -> conf['user_custom_title_lenght'] > 0)
		{
			// Set custom title
			$wrapper_vars['user_title'] = htmlspecialchars( $this -> appCore -> user['title']);
		}
		else
		{
			// Get title with special method
			$wrapper_vars['user_title'] = $this -> appCore -> users -> getRankTitle($this -> appCore -> user['user_rank'], $this -> appCore -> user['group_rank'], $this -> appCore -> user['posts']);
		}
		
		// Draw Path?
		if ( !empty( $path))
		{
			// Get number of items
			$path_n = count( $path);
			
			// Loop trought those items
			for ( $i = 0; $i < $path_n; $i ++)
			{
				$wrapper_vars['breadcrumbs'] .= $wrappers -> tpl_breadcrumb( array( 'link' => $path[$i][0], 'name' => $path[$i][1]));
			}
		}
		
		// Set User Avatar
		if ( $this -> appCore -> user['id'] > 0)
		{
			$wrapper_vars['avatar_type'] = $this -> appCore -> user['avatar_type'];
			$wrapper_vars['avatar_width'] = $this -> appCore -> user['avatar_width'];
			$wrapper_vars['avatar_height'] = $this -> appCore -> user['avatar_height'];
			
			// Get Image
			if ( $this -> appCore -> user['avatar_type'] == 2)
			{
				// Gallery avatar
				$wrapper_vars['avatar_image'] = AVATARS_GALLERY_PATH . $this -> appCore -> user['avatar_image'];
			
			}
			else if ( $this -> appCore -> user['avatar_type'] == 3)
			{
				// Uploaded Avatar
				$wrapper_vars['avatar_image'] = UPLOADS_PATH . $this -> appCore -> user['avatar_image'];
			}
			else
			{
				// Just avatar
				$wrapper_vars['avatar_image'] = $this -> appCore -> user['avatar_image'];
			}
		}
		
		// Draw Board Header
		echo $wrappers -> tpl_board_header( $wrapper_vars);
		
		// Draw Events?
		if ( !empty( $this -> events_tpl) && $draw_events)
		{
			// Draw Events before content
			echo $this -> events_tpl -> tpl_body_compact( array( 'errors' => $this -> msg_error, 'messages' => $this -> msg_misc));
		}
		
		// Page Content
		echo $content;
			
		// Draw Board Footer
		echo $wrappers -> tpl_board_footer( $wrapper_vars);
		
		// Draw Tasks
		echo $this -> appCore -> runTasks();
		
		// Draw Debugger?
		if ( $this -> appCore -> conf['board_debug_level'] == 2)
		{
			// Start Debugger
			$debug = new action_debug( $this -> appCore);
			
			// Draw It
			echo $debug -> draw();
		}
		
		// Close Wrapper
		echo $wrappers -> tpl_wrapper_end();
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Sends headers
	 *
	 * @param string $page_title
	 */
	
	function sendHeaders(){
		
		// Send no-cache headers?
		if ( $this -> appCore -> conf['server_send_nocache'])
		{
			//force no cache
			header( 'Cache-Control: no-cache, must-revalidate');
			header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT');
		}
		
		// Content type header
		header( 'Content-Type: text/html; charset=utf-8');	
		
		// Use GZIP?
		if ( $this -> appCore -> conf['board_use_gzip'])
		{
			ob_start("ob_gzhandler");
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws style image
	 *
	 * @param string $image_key
	 * @param string $image_alt
	 * @param integer $image_width
	 * @param integer $image_height
	 * @return string
	 */
	
	function drawImage( $image_key, $image_alt = '', $image_width = 0, $image_height = 0){

		//image exists?
		if ( isset( $this -> images[$image_key]))
		{
			// Use alternate string?
			if ( !empty( $image_alt))
			{
				// Fix html chars
				$image_alt = htmlspecialchars( $image_alt);
				
				// Return html tag string
				return '<img src="' . STYLES_DATA_PATH . $this -> images[$image_key]['file'] . '" alt="' . $image_alt . '" title="' . $image_alt . '" width="' . $this -> images[$image_key]['width'] . '" height="' . $this -> images[$image_key]['height'] . '" />';
			}
			else
			{
				// Return html tag string
				return '<img src="' . STYLES_DATA_PATH . $this -> images[$image_key]['file'] . '" alt="" title="" width="' . $this -> images[$image_key]['width'] . '" height="' . $this -> images[$image_key]['height'] . '" />';
			}
		}
		else
		{
			// Return NULL string
			return '';
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Sets style and imageset
	 *
	 * @param integer $style
	 * @param integer $imageset
	 */
	
	function setStyle( $style, $imageset){
		
		// Set style?
		if ( appCore::$style != $style)
		{
			// Set Style Id
			appCore::$style = $style;
			
			// Load Imageset from Cache
			$this -> images = (array) class_cache::loadCache( 'imageset_' .  $imageset);
			
			// Load Error?
			if ( class_cache::$load_error)
			{
				// Force type
				settype( $imageset, 'integer');
				
				// Select Imageset data
				$this -> appCore -> db -> query( 'SELECT image_name, image_file, image_width, image_height
				FROM ' . DB_PREFIX . 'imagesets_images
				WHERE imageset_id = \'' . $imageset . '\'');	
		
				// Pass from DB
				while ( $image_result = $this -> appCore -> db -> fetch_array())
				{
					$this -> images[$image_result[0]] = array(
						'file' => 'imageset_' . $imageset . '/' . $image_result[1],
						'width' => $image_result[2],
						'height' => $image_result[3]
					);
				}
				
				// Free DB result
				$this -> appCore -> db -> freeResult();
				
				// Save Cache
				class_cache::saveCache( 'imageset_' .  $imageset, $this -> images);
			}
			
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Sets event
	 *
	 * @param string $text
	 * @param string $title
	 */
	
	function setEvent( $text, $title = '', $information = true){
		
		// Templates loaded?
		if ( empty( $this -> events_tpl))
		{
			$this -> events_tpl = new tpl_events( $this -> appCore);
		}
		
		// Information event?
		if ( $information)
		{
			// Draw Message
			$this -> msg_misc .= $this -> events_tpl -> tpl_event_ok( array(
				'event_title' => $title,
				'event_text' => $text
			));
		}
		else
		{
			// Draw Error
			$this -> msg_error .= $this -> events_tpl -> tpl_event_error( array(
				'event_title' => $title,
				'event_text' => $text
			));
		}
		
	}

	// ------------------------------------------------------------------------
	
	/**
	 * Draws Page containing exclusively Events
	 *
	 * @param bool $forbid_login
	 * @param bool $in_error
	 */
	
	function drawEvents( $forbid_login = false, $in_error = true){
		
		// Templates loaded?
		if ( empty( $this -> events_tpl))
		{
			$this -> events_tpl = new tpl_events( $this -> appCore);
		}
		
		// Tpl Values
		$tpl_values = array(
			'in_error' 		=> $in_error,
			'errors' 		=> $this -> msg_error,
			'messages' 		=> $this -> msg_misc
		);
		
		// Handle Log-In thingie?
		if ( !$forbid_login && $this -> appCore -> user['id'] < 1)
		{
			// Show Log In form
			$tpl_values['allow_login'] = 1;
			
			// Fallback link
			$tpl_values['return_link'] = $_SERVER['REQUEST_URI'];
		}
		
		// Hide quick login
		$this -> show_login = false;
			
		// Draw output
		$this -> draw( $this -> appCore -> lang['board_message'], array(
			array( $this -> appCore -> link(), $this -> appCore -> lang['board_message']),
		), $this -> events_tpl -> tpl_body_full( $tpl_values), false);
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Draws redirect page
	 *
	 * @param string $redirect_link
	 * @param string $redirect_message
	 */
	
	function drawRedirect( $redirect_link = '', $redirect_message = ''){
				
		// Send redirect headers
		$this -> appCore -> setRedirect( $redirect_link);
		
		// Draw redirect page?
		if ( !$this -> appCore -> conf['redirections_instant'])
		{
			// Send basic headers
			$this -> sendHeaders();
			
			// Load templates
			$wrappers = new tpl_global( $this -> appCore);
			
			// Open Wrapper
			echo $wrappers -> tpl_wrapper_start( array(
				'page_css' 				=> htmlspecialchars( STYLES_DATA_PATH . 'style_' . appCore::$style . '.css'),
				'page_title' 			=> htmlspecialchars( isset( $page_title[0]) ? $this -> appCore -> conf['board_name'] . ' - ' . $page_title : $this -> appCore -> conf['board_name'] . (isset($this -> appCore -> conf['board_description'][0]) ? ' - ' . $this -> appCore -> conf['board_description'] : '')),
				'meta_keywords' 		=> ( isset( $this -> appCore -> conf['meta_keywords'][0]) ? htmlspecialchars( $this -> appCore -> conf['meta_keywords']) : ''),
				'meta_description' 		=> ( isset( $this -> appCore -> conf['meta_description'][0]) ? htmlspecialchars( $this -> appCore -> conf['meta_description']) : ''),
				'jquery_path'			=> JS_FRAMEWORK_PATH,
				'board_script_path'		=> JS_BOARD_PATH,
				'page_script'			=> htmlspecialchars( STYLES_DATA_PATH . 'script_' . appCore::$style . '.js'),
				'page_favicon' 			=> htmlspecialchars( isset( $this -> appCore -> conf['board_favicon'][0]) ? IMAGES_PATH . $this -> appCore -> conf['board_favicon'] : DEFAULT_FAVICON_PATH)
			));
			
			// Draw Redirect Page
			echo $wrappers -> tpl_redirect_page( array(
				'board_name' 				=> htmlspecialchars( $this -> appCore -> conf['board_name']),
				'board_description' 		=> htmlspecialchars( $this -> appCore -> conf['board_description']),
				'redirect_message' 			=> $redirect_message,
				'redirect_link'				=> $redirect_link,
				'board_footer'				=> $this -> appCore -> conf['board_footer'],
				'time'						=> $this -> appCore -> time,
				'board_show_version'		=> $this -> appCore -> conf['board_show_version'],
				'board_version'				=> PRODUCT_VERSION_LABEL
			));
			
			// Close Wrapper
			echo $wrappers -> tpl_wrapper_end();
			
		}
		
	}

}